/************************************************************************/
/* @file            utilParam.h                                         */
/* @brief           gxParam related APIs                                */
/* @version         2013.11.14 johnxguo created                         */
/************************************************************************/

#ifndef __UTIL_PARAM_H_INCLUDED__
#define __UTIL_PARAM_H_INCLUDED__

#include "IGxParam.h"

namespace gx
{
namespace base
{
bool CreateGxParam(CGxPtr<IGxParam>& pParam);

template<class T>
bool SetParam(CGxPtr<IGxParam> gxpParam, const String& strKey, T valT, u8 cType)
{
    if (!gxpParam)
    {
        return false;
    }

    T* p = new T(valT);
    if (gxpParam->Insert(strKey, cType, (void*)p))
    {
        return true;
    }

    delete p;
    return false;
}

template<class T>
bool SetParam(CGxPtr<IGxParam> gxpParam, const String& strKey, CGxPtr<T> gxpT)
{
    if (!gxpParam)
    {
        return false;
    }

    if (gxpParam->Insert(strKey, GX_DATA_TYPE_GXPTR, (void*)(T*)gxpT))
    {
        gxpT->AddRef();
        return true;
    }

    return false;
}

template<class T>
bool SetParam(CGxPtr<IGxParam> gxpParam, const String& strKey, CGxPtr<IGxParam> gxpInParam)
{
    if (!gxpParam)
    {
        return false;
    }

    if (gxpParam->Insert(strKey, GX_DATA_TYPE_PARAM, (void*)(IGxParam*)gxpInParam))
    {
        gxpInParam->AddRef();
        return true;
    }

    return false;
}

template<class T>
bool GetParam(CGxPtr<IGxParam> gxpParam, const String& strKey, T& valT, u8 cType)
{
    if (!gxpParam)
    {
        return false;
    }

    void* pVoid = NULL;
    if (!gxpParam->Obtain(strKey, cType, pVoid))
    {
        return false;
    }
    if (pVoid)
    {
        valT = *((T*)pVoid);
        return true;
    }

    return false;
}

template<class T>
bool GetParam(CGxPtr<IGxParam> gxpParam, const String& strKey, CGxPtr<T>& gxpT)
{
    if (!gxpParam)
    {
        return false;
    }

    void* pVoid = NULL;
    if (!gxpParam->Obtain(strKey, GX_DATA_TYPE_GXPTR, pVoid))
    {
        return false;
    }

    gxpT = (T*)pVoid;
    return true;
}

template<class T>
bool GetParam(CGxPtr<IGxParam> gxpParam, const String& strKey, CGxPtr<IGxParam>& gxpInParam)
{
    if (!gxpParam)
    {
        return false;
    }

    void* pVoid = NULL;
    if (!gxpParam->Obtain(strKey, GX_DATA_TYPE_PARAM, pVoid))
    {
        return false;
    }

    gxpInParam = (IGxParam*)pVoid;
    return true;
}

}//end namespace base
}//end namespace gx

#endif